package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.FileInfo;
import com.chzc.fm.duotin.utils.NumberUtil;

import java.util.List;

/**
 * Created by BillSinla on 2016/7/8.
 */
public class DownloadRecyclerAdapter extends RecyclerView.Adapter<DownloadRecyclerAdapter.DownloadHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<FileInfo> fileInfoList;

    public DownloadRecyclerAdapter(Context context, List<FileInfo> fileInfoList) {
        this.context = context;
        this.fileInfoList = fileInfoList;
        inflater = LayoutInflater.from(context);
    }

    public void setFileInfoList(List<FileInfo> fileInfoList) {
        this.fileInfoList = fileInfoList;
        notifyDataSetChanged();
    }

    @Override
    public DownloadHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_down_item, null);
        return new DownloadHolder(view);
    }

    @Override
    public void onBindViewHolder(DownloadHolder holder, int position) {
        holder.bindView(position);
    }

    @Override
    public int getItemCount() {
        return fileInfoList != null ? fileInfoList.size() : 0;
    }

    public interface OnStateChangeListener{
        void onStateChanged();
    }

    public class DownloadHolder extends RecyclerView.ViewHolder {
        private TextView textTitle, textSpace, textTip, textCheck;
        private ProgressBar progressBar;
        public DownloadHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.tv_download_title);
            textSpace = (TextView) itemView.findViewById(R.id.tv_download_space);
            textTip = (TextView) itemView.findViewById(R.id.tv_download_tip);
            textCheck = (TextView) itemView.findViewById(R.id.tv_download_check);

            progressBar = (ProgressBar) itemView.findViewById(R.id.pb_download_show);
            progressBar.setMax(100);
        }

        public void bindView(int position){
            FileInfo fileInfo = fileInfoList.get(position);
            textTitle.setText(fileInfo.getFileName());
            textSpace.setText("0.0MB/" + NumberUtil.countSpace(fileInfo.getLength()));
        }

    }
}
